#include "LibraryWidget.h"
#include <QPainter>
#include <QDebug>

LibraryWidget::LibraryWidget(int size, QWidget *parent) :
	QWidget(parent), size(size)
{
	books = new Qt::GlobalColor[size];
	for (int i = 0; i < size; i++) {
		books[i] = Qt::transparent;
	}
	setFixedHeight(rectSize * 5 + 90);
	setFixedWidth(size * rectSize + 50);
}

void LibraryWidget::paintEvent(QPaintEvent *)
{
	QPainter p(this);

	p.setBrush(Qt::green);
	p.drawRect(QRect(25, 25, rectSize, rectSize));
	p.drawText(60, 35, "free book");
	p.setBrush(Qt::yellow);
	p.drawRect(QRect(25, 60, rectSize, rectSize));
	p.drawText(60, 70, "reading book");
	p.setBrush(Qt::red);
	p.drawRect(QRect(25, 95, rectSize, rectSize));
	p.drawText(60, 105, "editing book");
	p.setBrush(Qt::transparent);
	p.drawRect(QRect(25, 130, rectSize, rectSize));
	p.drawText(60, 140, "no book");

	for (int i = 0; i < size; i++) {
		QRect rect(25 + i * rectSize, 165, rectSize, rectSize);
		p.setBrush(books[i]);
		p.drawRect(rect);
	}
}

void LibraryWidget::freeBook(int id)
{
	books[id] = Qt::green;
	repaint();
}

void LibraryWidget::readBook(int id)
{
	books[id] = Qt::yellow;
	repaint();
}

void LibraryWidget::editBook(int id)
{
	books[id] = Qt::red;
	repaint();
}
